package com.jt.tenant.viewpager;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TextView;
import com.jt.tenant.R;
import com.jt.tenant.model.PicModel;

import java.util.ArrayList;
import java.util.List;

public class ImagePagerActivity extends FragmentActivity {
	private static final String STATE_POSITION = "STATE_POSITION";
	public static final String EXTRA_IMAGE_INDEX = "image_index";
	public static final String EXTRA_IMAGE_URLS = "image_urls";

	private HackyViewPager mPager;
	private int pagerPosition;
	private TextView indicator;
	private List<PicModel> mLists = new ArrayList<>();
	private String flag = "0";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_detail_pager);

		pagerPosition = getIntent().getExtras().getInt(EXTRA_IMAGE_INDEX);
		mLists = (List<PicModel>) getIntent().getExtras().getSerializable(EXTRA_IMAGE_URLS);
		flag = getIntent().getExtras().getString("flag","0");
		if(flag.equals("0")){
			if(mLists.size()==6&&mLists.get(5).isPic){

			}else{
				mLists.remove(mLists.size()-1);
			}
		}else if(flag.equals("1")){
			if(mLists.size()==2&&mLists.get(1).isPic){

			}else{
				mLists.remove(mLists.size()-1);
			}
		}


//		String[] urls = getIntent().getStringArrayExtra(EXTRA_IMAGE_URLS);


		mPager = (HackyViewPager) findViewById(R.id.pager);
		ImagePagerAdapter mAdapter = new ImagePagerAdapter(
				getSupportFragmentManager(), mLists);
		mPager.setAdapter(mAdapter);
		indicator = (TextView) findViewById(R.id.indicator);

		CharSequence text = getString(R.string.viewpager_indicator, 1, mPager
				.getAdapter().getCount());
		indicator.setText(text);
		// 更新下标
		mPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageSelected(int arg0) {
				CharSequence text = getString(R.string.viewpager_indicator,
						arg0 + 1, mPager.getAdapter().getCount());
				indicator.setText(text);
			}

		});
		if (savedInstanceState != null) {
			pagerPosition = savedInstanceState.getInt(STATE_POSITION);
		}

		mPager.setCurrentItem(pagerPosition);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putInt(STATE_POSITION, mPager.getCurrentItem());
	}

	private class ImagePagerAdapter extends FragmentStatePagerAdapter {

//		public String[] fileList;

		public List<PicModel> mPicLists;

		public ImagePagerAdapter(FragmentManager fm, List<PicModel> list) {
			super(fm);
//			this.fileList = fileList;

			mPicLists = list;
		}

		@Override
		public int getCount() {
			return mPicLists == null ? 0 : mPicLists.size();
		}

		@Override
		public Fragment getItem(int position) {
			String url = mPicLists.get(position).picUrl;
			return ImageDetailFragment.newInstance(url);
		}

	}
}